<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 3月27日
 * Time: 18:16
 */
namespace app\api\controller;

use think\Request;

class Offer
{
    /*
     * 新增offer
     */
    public function addOffer($param,$advertiser_id)
    {
        $username = 'idvert';
        $security_token = '7658532c77854965ae4ebc90f6ae8d09';

        $url = 'http://idvert.api.offerslook.com/v1/offers';

        //$advertiser_id = 72;//广告主ID

        /*$param = array(
            'name' => 'TEST_Gett - The Best Black Cabs',
            'preview_url' => 'https://play.google.com/store/apps/details?id=com.gettaxi.android',
            'destination_url' => 'http://ads.dofunapps.com/acs.php?sid=6362&adid=7792014',
            'description' => $param['description'],
            'offer_id' => 7792014,
            'price' => 0.2,
            'os' => 'ios',
            'os_version' =>'',
            'country' => array('GBR'),
        );*/


        $data = array();
        $data['offer'] = array(
            'name' => $param['name'],
            'advertiser_id' => $advertiser_id,
            'offer_approval' => 2,
            'revenue_type' => 'RPA',
            'revenue' => $param['price'],
            'payout_type' => 'CPA',
            'payout' => number_format($param['price'] * 0.8,2),
            'preview_url' => $param['preview_url'],
            'destination_url' => $param['destination_url'],
            'description' => $param['description'],
            'advertiser_offer_id' => $param['offer_id'],
            'hide_referral' => 1,
            'status' => $param['status'] == 1 ? 'active' : 'paused',
        );
        $os_target = $garget_data = array();
        if($param['os'] == 'ios'){
            $os_target['platform'] = 'Mobile';
            $os_target['system'] = 'iOS';
            $os_target['version'] = $param['os_version'] == 0 ? array() : (array)$param['os_version'];
            $os_target['is_above'] = 1;

        } elseif($param['os'] == 'android'){
            $os_target['platform'] = 'Mobile';
            $os_target['system'] = 'Android';
            $os_target['version'] = $param['os_version'] == 0 ? array() : (array)$param['os_version'];
            $os_target['is_above'] = 1;
        }
        $garget_data[] = $os_target;
        $data['offer_platform'] = array(
            'target' => $garget_data,
        );
        //$data['offer_platform'] = $os_target;

        $data['offer_category']['name'] = 'API';

        $target = array();
        foreach($param['country'] as $tk => $tv){
            $target[$tk]['type'] = 1;
            $target[$tk]['country'] = $tv;
        }
        $data['offer_geo'] = array(
            'type' => 1,
            'target' => $target,
        );
        $data['offer_cap'] = array(
            'adv_cap_type' => 2,
            'adv_cap_click' => 1,
            'adv_cap_conversion' => (isset($param['day_cap']) && !empty($param['day_cap'])) ? $param['day_cap'] : 200,
            'adv_cap_revenue' => 1,
            'aff_cap_type' => 2,
            'aff_cap_click' => 1,
            'aff_cap_conversion' => (isset($param['day_cap']) && !empty($param['day_cap'])) ? $param['day_cap'] : 200,
            'aff_cap_payout' => 1
        );
        //dump($data);
        //echo json_encode($data);

        $res = curl_send($url,$data,$username,$security_token);


        return json_decode($res,true);
        //echo $a;

    }

    /*
     * 更新offer
     * $offer_id offerslook平台的offerid
     * $advertiser_id 广告主ID
     */
    public function updateOffer($offer_id,$advertiser_id,$param)
    {

        $username = 'idvert';
        $security_token = '7658532c77854965ae4ebc90f6ae8d09';

        $url = 'http://idvert.api.offerslook.com/v1/offers/'.$offer_id;

        //$advertiser_id = 72;//广告主ID

        /*$param = array(
            'name' => 'TEST_Gett - The Best Black Cabs',
            'preview_url' => 'https://play.google.com/store/apps/details?id=com.gettaxi.android',
            'destination_url' => 'http://ads.dofunapps.com/acs.php?sid=6362&adid=7792014',
            'offer_id' => 7792014,
            'price' => 0.2,
            'os' => 'ios',
            'os_version' =>'',
            'country' => array('GBR'),
        );*/


        $data = array();
        $data['offer'] = array(
            'name' => $param['name'],
            'advertiser_id' => $advertiser_id,
            'offer_approval' => 2,
            'revenue_type' => 'RPA',
            'revenue' => $param['price'],
            'payout_type' => 'CPA',
            'payout' => number_format($param['price'] * 0.8,2),
            'preview_url' => $param['preview_url'],
            'destination_url' => $param['destination_url'],
            'description' => $param['description'],
            'advertiser_offer_id' => $param['offer_id'],
            'status' => $param['status'] == 1 ? 'active' : 'paused',
        );
        $os_target = $garget_data = array();
        if($param['os'] == 'ios'){
            $os_target['platform'] = 'Mobile';
            $os_target['system'] = 'iOS';
            $os_target['version'] = $param['os_version'] == 0 ? array() : (array)$param['os_version'];
            $os_target['is_above'] = 1;

        } elseif($param['os'] == 'android'){
            $os_target['platform'] = 'Mobile';
            $os_target['system'] = 'Android';
            $os_target['version'] = $param['os_version'] == 0 ? array() : (array)$param['os_version'];
            $os_target['is_above'] = 1;
        }
        $garget_data[] = $os_target;
        $data['offer_platform'] = array(
            'target' => $garget_data,
        );
        //$data['offer_platform'] = $os_target;

        //$data['offer_category']['name'] = 'Non-incentive Traffic';

        $target = array();
        foreach($param['country'] as $tk => $tv){
            $target[$tk]['type'] = 1;
            $target[$tk]['country'] = $tv;
        }
        $data['offer_geo'] = array(
            'type' => 1,
            'target' => $target,
        );
        /*$data['offer_cap'] = array(
            'adv_cap_type' => 3,
            'adv_cap_click' => 1,
            'adv_cap_conversion' => 100,
            'adv_cap_revenue' => 200,
            'aff_cap_type' => 3,
            'aff_cap_click' => 1,
            'aff_cap_conversion' => 1,
            'aff_cap_payout' => 200
        );*/



        $res = curl_send($url,$data,$username,$security_token);


        return json_decode($res,true);
    }

    public function updateOfferPrice($offer_id,$price)
    {
        $username = 'idvert';
        $security_token = '7658532c77854965ae4ebc90f6ae8d09';

        $url = 'http://idvert.api.offerslook.com/v1/offers/'.$offer_id;

        $data = array();
        $data['offer'] = array(
            'status' => 'active',
            'revenue' => $price,
            'payout' => number_format($price * 0.8,2),
        );

        $res = puturl($url,$data,$username,$security_token);

        return json_decode($res,true);
    }

    /*
     * 下线offer
     */
    public function pausedOffer($ids,$status = 'paused')
    {
        $id_str = implode(',',$ids);
        $username = 'idvert';
        $security_token = '7658532c77854965ae4ebc90f6ae8d09';

        $url = 'http://idvert.api.offerslook.com/v1/batches/offers/'.$id_str.'?status='.$status;
        $data = array();
        $res = patchurl($url,$data,$username,$security_token);

        return $res;
        //return json_decode($res,true);

    }

    /*
     * 上传logo
     */
    public function uploadLogo($offer_id,$path)
    {
        $url = 'http://idvert.api.offerslook.com/v1/offers/'.$offer_id.'/thumbnails';
        $res = curl_upload($url,$path);
        return $res;
    }
}